# 集合

using system.collection
using system.collection.Generic
ArrayList list<T>
Hashtable Dictionary<k,y>
堆栈   stack stack<T> 先进后出
队列 queue queue<T> 先进先出
sortlist sortlist<k,v> sorteddictionary<k.v> 插入检索没Dictionary高效,但可以排序
sort方法根据Icompararble接口的默认方法
sort(icomparer)用传进来的比较器进行比较
foreach(object item in hash.keys)
Hashtable :foreach(dictionaryentry item in hash)
Dictionary:foreach(keyvaluepair<string,int> item in hash)
1
2
3
4
5
6
7
8
9
10
11
12

# 泛性

泛型类
class my<T>{}
泛型方法
普通类中有泛型方法
class my1
{
public T say<T>(T tt){}
}
泛型接口
inerface IFace<T>
{
}
实例类是确定T类型,接口t由类中的T确定
class my2<T>:IFace<T>{}
where T : struct                               | T必须是一个结构类型值类型
where T : class                                | T必须是一个Class类型引用类型
where T : new()                               | T必须要有一个无参构造函数
where T : NameOfBaseClass          | T必须继承名为NameOfBaseClass的类或子类
where T : NameOfInterface             | T必须实现名为NameOfInterface的接口
where T : class, new()                  | T必须是一个Class类型引用类型并且具有无参构造函数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

# yield 生产——foreach的语法糖

只能在迭代器块中
yield return 表达式
yield break
返回Ienumerable表示自动生产一个可迭代的类型(类)
返回Ienumertor表示自动生产一个迭代器(方法)
1
2
3
4
5

# foreach(只读)

  • 可枚举类型(具有GetEnumerator()方法)
  • 枚举器(具有iEnumerator接口中成员的类)
foreach(var item in arr)
{}

其实是先执行一遍GetEnumerator(),获取枚举器
接下来在每一次循环中调用枚举器类(IEnumerator)的MoveNext();方法
直到返回false
1
2
3
4
5
6

从而进一步我们可以了解yield

foreach(var item in arr)
{}
先获取通过GetEnumerator()获取IEnumerato枚举器

yield代替了MoveNext()方法
yield就是MoveNext的语法糖。


yield return 1;
yield return 2;
yield break;
yield return 3;

执行1后,再次循环执行2,再次循环跳过,再次循环执行3,结束

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
例子随手写的,可以查网络上的其他例子看
static void Main(string[] args)
{
    Console.WriteLine("Hello World!");
    Person p = new Person();
    IEnumerator enumerator = p.GetEnumerator();
    enumerator.MoveNext();
    Console.WriteLine(enumerator.Current.ToString()); 
    enumerator.MoveNext();
    Console.WriteLine(enumerator.Current.ToString());
    enumerator.MoveNext();
    Console.WriteLine(enumerator.Current.ToString());
    Console.ReadKey();
}

非泛型
public class Person:IEnumerable
{
    private readonly string[] person = new string[] 
    { 
        "123", "234", "567", "789", "6757" 
    };
    //获得枚举器
    public IEnumerator GetEnumerator()
    {
        return new PersonIEnumerator(this.person);
    }
}

 
public class PersonIEnumerator : IEnumerator
{
    private string[] _friends;
    public PersonIEnumerator(string[] fs)
    {
        _friends = fs;
    }
    private int index = -1;
    public object Current {
    get {
        if (index + 1 < _friends.Length-1)
        {
            return _friends[index];
        }
        return null;
    }
}

public bool MoveNext()
{
    if (index+1<_friends.Length)
    {
        index++;
        return true;
    }
    return false;
}
    public void Reset()
    {
        index = -1;
    }
}

泛型
public class Person:IEnumerable<string>
{
    private readonly string[] person = new string[] 
    { "123", "234", "567", "789", "6757" };

    public IEnumerator<string> GetEnumerator()
    {
        throw new NotImplementedException();
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        throw new NotImplementedException();
    }
    
    //获得枚举器
    //public IEnumerator GetEnumerator()
    //{
    //    return new PersonIEnumerator(this.person);
    //}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
static void Main(string[] args)
{
    Console.WriteLine("Hello World!");
    Person p = new Person();
    1.
    foreach (var obj in p.GetObject())
    {
    }
    2.
    foreach (var obj in p)
    {
    }
    var arr=p.GetObject()
    3.
    arr.MoveNext()
    arr.MoveNext()
    arr.MoveNext()
}

public class Person
{
    private readonly string[] person = new string[] 
    { "123", "234", "567", "789", "6757" };

    public IEnumerable GetObject()
    {
        for(int i=0;i<person.Length;i++)
        {
            yield return person[i];
        }
    }
}

public class Person
{
    private readonly string[] person = new string[]
    { "123", "234", "567", "789", "6757" };

    public IEnumerator GetEnumerator()
    {
        for(int i=0;i<person.Length;i++)
        {
            yield return person[i];
        }
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46